package com.llp.javaweb.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.llp.javaweb.pojo.AddProduct;
import com.llp.javaweb.service.ProductService;
import com.llp.javaweb.service.impl.ProductServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @BelongsProject: AscentWeb
 * @BelongsPackage: com.llp.javaweb.servlet
 * @Author: bovan
 * @Date: 2022/11/2 09:23
 * @Description:
 */
@WebServlet("/admin/addproduct")
public class AddProductsServlet extends HttpServlet {
    ProductService service = new ProductServiceImpl();
    /*
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        AddProduct product= new ObjectMapper().readValue(req.getInputStream(), AddProduct.class);
        Boolean bool = service.addProduct(resp, product);
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        if(bool == true){
            resp.setStatus(200);
            resp.getWriter().write("添加商品成功");
        }else{
            resp.setStatus(400);
            resp.getWriter().write("添加商品失败");
        }
    }

     */

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
         req.getRequestDispatcher("/WEB-INF/templates/product/add_products_admin.html").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        AddProduct product= new ObjectMapper().readValue(req.getInputStream(), AddProduct.class);
        Boolean bool = service.addProduct(resp, product);
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        if(bool == true){
            resp.setStatus(200);
            resp.getWriter().write("添加商品成功");
        }else{
            resp.setStatus(400);
            resp.getWriter().write("添加商品失败");
        }
    }
}
